package com.work.rcj.common.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.work.rcj.common.exception.ErrorEnum;
import com.work.rcj.common.exception.ValidateException;
import com.work.rcj.common.model.page.PageResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;

/**
 * @author yy
 * @Type BaseController.java
 * @Desc
 * @date 2020/8/11 23:34
 */
public class BaseController<T> {

    public void checkParameters(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (ObjectError object : bindingResult.getAllErrors()) {
                String message = object.getDefaultMessage();
                throw new ValidateException(ErrorEnum.BODY_NOT_MATCH.getErrorCode(), message);
            }
        }
    }

    public PageResult<T> getDataTable(IPage<T> iPage) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setPageData(iPage.getRecords());
        pageResult.setPageSize(iPage.getSize());
        pageResult.setPageIndex(iPage.getPages());
        pageResult.setTotalCount(iPage.getTotal());
        return pageResult;
    }
}
